<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2006/08/01 19:05:01 $)
// $RCSfile: client_troubleshooter.php,v $ : $Revision: 1.11 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

require_once ("./includes/functions_comments.php");

// ======= PERMISSION CHECK =======
if ($_SWIFT["user"]["permissions"]["perm_canviewtroubleshooter"] == "0" || $_SWIFT["settings"]["tr_enableclient"] == "0")
{
	$template->assign("errormessage", $_SWIFT["language"]["perminvalid"]);
	echo $template->displayTemplate("header");
	echo $template->displayTemplate("footer");
	exit;
}

/**
* ###############################################
* MAIN CLIENT AREA CATEGORY DISPLAY
* ###############################################
*/
if ($eventaction == "view")
{
	$template->cache(array('tscategorydisplay', 'header', 'footer', 'navbar'));

	$troubleshootercatlist = $troubleshootercatidlist = array();
	$trcount = $index = 0;
	$dbCore->query("SELECT troubleshootercat.* FROM `". TABLE_PREFIX ."troubleshootercat` AS troubleshootercat LEFT JOIN `". TABLE_PREFIX ."tgroupassigns` AS tgroupassigns ON (troubleshootercat.troubleshootercatid = tgroupassigns.toassignid AND tgroupassigns.type = '". TGROUP_TROUBLESHOOTER ."') WHERE tgroupassigns.tgroupid = '". intval($_SWIFT["tgroup"]["tgroupid"]) ."' ORDER BY troubleshootercat.displayorder ASC;");
	while ($dbCore->nextRecord())
	{
		$trcount++;
		$troubleshootercatlist[$index] = $dbCore->Record;
		$troubleshootercatlist[$index]["number"] = $trcount;
		$troubleshootercatlist[$index]["description"] = getProcessedHTML($dbCore->Record["description"]);
		$index++;
	}

	if (count($trcount))
	{
		$template->assign("iscat", "1");
		$template->assign("tscatlist", $troubleshootercatlist);
	}

	$template->assign("navigation", '<a href="index.php" id="navlink">'.$_SWIFT["language"]["navhome"].'</a> &raquo; <a href="index.php?_m=troubleshooter&_a=view" id="navlink">'.$_SWIFT["language"]["troubleshooter"]."</a>");

	$template->assign("sectiontitle", $_SWIFT["language"]["troubleshootercat"]);
	$template->assign("sectiondesc", $_SWIFT["language"]["troubleshooterdesc"]);
	$template->assign("backurl", "index.php");

	echo $template->displayTemplate("tscategorydisplay");



/**
* ###############################################
* STEPS
* ###############################################
*/
} else if ($eventaction == "steps") {
	$template->cache(array('tsstepdisplay', 'tsnavbar', 'header', 'footer', 'navbar'));
	// First get the details of category
	$_troubleshootercat = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."troubleshootercat` WHERE `troubleshootercatid` = '". intval($_REQUEST["troubleshootercatid"]) ."';");
	if (empty($_troubleshootercat["troubleshootercatid"]))
	{
		trigger_error($_SWIFT["language"]["invalidtroubleshootercat"], E_USER_ERROR);
	}
	$template->assign("troubleshootercatid", $_troubleshootercat["troubleshootercatid"]);

	// Ok, Now get the list of ALL Troubleshooter Categories for this User Group
	$tscatlist = array();
	$dbCore->query("SELECT troubleshootercat.* FROM `". TABLE_PREFIX ."troubleshootercat` AS troubleshootercat LEFT JOIN `". TABLE_PREFIX ."tgroupassigns` AS tgroupassigns ON (troubleshootercat.troubleshootercatid = tgroupassigns.toassignid AND tgroupassigns.type = '". TGROUP_TROUBLESHOOTER ."') WHERE tgroupassigns.tgroupid = '". intval($_SWIFT["tgroup"]["tgroupid"]) ."' ORDER BY troubleshootercat.displayorder ASC;");
	while ($dbCore->nextRecord())
	{
		$tscatlist[] = $dbCore->Record;
	}
	if ($_SWIFT["settings"]["tr_displaynavbar"] == 1)
	{
		$template->assign("inctrnav", true);
	}
	$template->assign("tsnavcatlist", $tscatlist);
	
	if (!empty($_REQUEST["backid"]))
	{
		$backidarray = unserialize($_REQUEST["backid"]);
	} else {
		$backidarray = array();
	}

	if (htmlspecialchars($_POST["action"]) == $_SWIFT["language"]["tsstartover"])
	{
		// Redirect this person to troubleshooter main page
		header("location: index.php?_m=troubleshooter&_a=view");
		exit;
	} else if (htmlspecialchars($_POST["action"]) == $_SWIFT["language"]["tsback"] && !empty($_REQUEST["backid"])) {
		// We need to go back!
		if ($backidarray[$_REQUEST["backidpass"]] != "")
		{
			$_REQUEST["parenttroubleshooterid"] = $backidarray[$_REQUEST["backidpass"]];
		}

		if ($_REQUEST["parenttroubleshooterid"] == -1)
		{
			$_REQUEST["parenttroubleshooterid"] = 0;
		}
		$nobackidpass = true;
	}

	$template->assign("parenttroubleshooterid", intval($_REQUEST["parenttroubleshooterid"]));

	// Is the parentid empty?
	unset($parentid);
	if (empty($_REQUEST["parenttroubleshooterid"]))
	{
//		$_troubleshootercatdata = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."troubleshootercatdata` WHERE `troubleshootercatid` = '". intval($_troubleshootercat["troubleshootercatid"]) ."';");
		// Yes. So we set subject to that of our category
		$template->assign("subject", $_troubleshootercat["title"]);
		$template->assign("contents", getProcessedHTML($_troubleshootercat["description"]));
		$parentid = 0;
		$template->assign("backidpass", "-1");
		$backidarray[-1] = 0;
		
		$dbCore->shutdownQuery("UPDATE LOW_PRIORITY `". TABLE_PREFIX ."troubleshootercat` SET `views` = `views`+1 WHERE `troubleshootercatid` = '". intval($_troubleshootercat["troubleshootercatid"]) ."';");

	} else {
		$dbCore->shutdownQuery("UPDATE LOW_PRIORITY `". TABLE_PREFIX ."troubleshootersteps` SET `views` = `views`+1 WHERE `troubleshooterid` = '". intval($_REQUEST["parenttroubleshooterid"]) ."';");

		// Get this current troubleshooter item
		$_troubleshooter = $dbCore->queryFetch("SELECT * FROM `". TABLE_PREFIX ."troubleshootersteps` AS troubleshootersteps LEFT JOIN `". TABLE_PREFIX ."troubleshooterdata` AS troubleshooterdata ON (troubleshootersteps.troubleshooterid  = troubleshooterdata.troubleshooterid) WHERE troubleshootersteps.troubleshooterid = '". intval($_REQUEST["parenttroubleshooterid"]) ."';");
		if ($_troubleshooter["troubleshootercatid"] != $_troubleshootercat["troubleshootercatid"])
		{
			trigger_error($_SWIFT["language"]["invalidtroubleshootercat"], E_USER_ERROR);
		} else if (empty($_troubleshooter["troubleshooterid"])) {
			trigger_error($_SWIFT["language"]["invalidtroubleshooter"], E_USER_ERROR);
		}

		$template->assign("subject", $_troubleshooter["subject"]);
		$template->assign("contents", getProcessedHTML($_troubleshooter["contents"]));
		$parentid = $_troubleshooter["troubleshooterid"];
		if (!$nobackidpass)
		{
			$backidarray[$parentid] = $_REQUEST["backidpass"];
		}
		$template->assign("backidpass", $parentid);
	}
	$serializeddata = serialize($backidarray);
	$template->assign("backid", $serializeddata);

	renderClientComments(COMMENT_TROUBLESHOOTER, intval($_REQUEST["parenttroubleshooterid"]));

	// Ok now get all the links
	$troubleshooterlinks = array();
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."troubleshooterlinks` WHERE `troubleshootercatid` = '". intval($_troubleshootercat["troubleshootercatid"]) ."' AND `parenttroubleshooterid` = '". intval($parentid) ."';");
	while ($dbCore->nextRecord())
	{
		$troubleshooterlinks[] = $dbCore->Record["childtroubleshooterid"];
	}

	// Ok now get all the child steps
	$troubleshooterchildsteps = array();

	//MAHESH: This displays troubleshooter steps in ascending diasplay order. 
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."troubleshootersteps` WHERE `troubleshooterid` IN (". buildIN($troubleshooterlinks) .") ORDER BY displayorder ASC;");
	$firstone = false;
	$index = 0;
	while ($dbCore->nextRecord())
	{
		$troubleshooterchildsteps[$index] = $dbCore->Record;
		if (!$firstone)
		{
			$troubleshooterchildsteps[$index]["checked"] = true;
			$firstone = true;
		}
		$index++;
	}

	$template->assign("steplist", $troubleshooterchildsteps);
	$template->assign("stepcount", count($troubleshooterchildsteps));

	if ($parentid)
	{
		$template->assign("backbutton", true);
	}


	if (count($troubleshooterchildsteps))
	{
		$template->assign("nextbutton", true);
	}
	$template->assign("startoverbutton", true);
	
	$template->assign("navigation", '<a href="index.php" id="navlink">'.$_SWIFT["language"]["navhome"].'</a> &raquo; <a href="index.php?_m=troubleshooter&_a=view" id="navlink">'.$_SWIFT["language"]["troubleshooter"].'</a> &raquo; <a href="index.php?_m=troubleshooter&_a=steps&troubleshootercatid='. $_troubleshootercat["troubleshootercatid"] .'&parentid=0" id="navlink">'.$_troubleshootercat["title"].'</a>');

	echo $template->displayTemplate("tsstepdisplay");
}
?>